package array.easy;

/**
 * 125.验证回文串
 */
public class isPalindrome {

    public static void main(String[] args) {
        String s = "A man, a plan, a canal: Panama";
        System.out.println(myCode1(s));

    }

    /**
     * 对撞指针
     *
     * @param s
     * @return
     */
    public static boolean myCode(String s) {
        if (s.length() != 0) {
            String ns = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
            int p1 = 0;
            int p2 = ns.length() - 1;
            while (p1 < p2) {
                if (ns.charAt(p1) != ns.charAt(p2)) {
                    return false;
                }
                p1++;
                p2--;
            }
        }
        return true;
    }

    /**
     * 对撞指针2 原地比较
     *
     * @param s
     * @return
     */
    public static boolean myCode1(String s) {
        int p1 = 0;
        int p2 = s.length() - 1;
        if (s.length() != 0) {
            while (p1 < p2) {

                while (p1 < p2 && !Character.isLetterOrDigit(s.charAt(p1))) {
                    ++p1;
                }
                while (p1 < p2 && !Character.isLetterOrDigit(s.charAt(p2))) {
                    --p2;
                }
                if (p1 < p2) {
                    if (Character.toLowerCase(s.charAt(p1)) != Character.toLowerCase(s.charAt(p2))) {
                        return false;
                    }
                    ++p1;
                    --p2;
                }
            }
        }
        return true;
    }
}
